package com.shuang.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.shuang.mapper.DbGoodsPropertyMapper;
import com.shuang.pojo.DbGoodsProperty;
import com.shuang.pojo.DbGoodsPropertyExample;
import com.shuang.pojo.DbGoodsPropertyExample.Criteria;
import com.shuang.pojo.ExtendDbGoodsProperty;
import com.shuang.service.DbGoodsPropertyService;
@Service("dbGoodsPropertyService")
public class DbGoodsPropertyServiceImpl implements DbGoodsPropertyService {

	@Resource(name="dbGoodsPropertyMapper")
	private DbGoodsPropertyMapper dbGoodsPropertyMapper;
	
	@Override
	public List<ExtendDbGoodsProperty> findPropertyById(Long cId) {
		DbGoodsPropertyExample example = new DbGoodsPropertyExample();
		Criteria criteria = example.createCriteria();
		criteria.andCIdEqualTo(cId);
		return dbGoodsPropertyMapper.selectExtendByExample(example);
		 
	}

	@Override
	public Map<String, Object> savePropertyParam(Long cId, String paramData) {
		DbGoodsProperty record = new DbGoodsProperty();
		record.setcId(cId);
		record.setGpParamData(paramData);
		record.setGpCreateTime(new Date());
		int result = dbGoodsPropertyMapper.insert(record);
		 
		 Map<String, Object> map=new HashMap<String, Object>();
		 if(result>0){
			  map.put("status", 200);
			  
		 }else{
			 map.put("status", 500);
		 }
		 return map;
	}

	@Override
	public Map<String, Object> updatePropertyParam(Long gpId, String paramData) throws Exception {
		DbGoodsProperty record = new DbGoodsProperty();
		record.setGpParamData(paramData);
		record.setGpUpdateTime(new Date());
		record.setGpId(gpId);
		int result = dbGoodsPropertyMapper.updateByPrimaryKeySelective(record);
		Map<String,Object> map=new HashMap<String, Object>();
		if(result>0){
			map.put("status", 200);
		}else{
			map.put("status", 500);
			throw new Exception();
		}
		return map;
	}

	@Override
	public Map<String, Object> selectPropertyInfoByCid(Long cId) {
		DbGoodsPropertyExample example = new DbGoodsPropertyExample();
		Criteria criteria = example.createCriteria();
		criteria.andCIdEqualTo(cId);
		List<DbGoodsProperty> result = dbGoodsPropertyMapper.selectByExample(example);
		Map<String,Object> map=new HashMap<String, Object>();
		if(result!=null&&result.size()>0){
			map.put("status", 200);
			map.put("data",result.get(0) );
		}else{
			map.put("status", 400);
		}
		return map;
	}

}
